cmake_minimum_required(VERSION 3.10)
project(cgzGoing)

# 设置 C++ 编译标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置编译选项
set(CMAKE_CXX_FLAGS "-Wno-misleading-indentation -rdynamic -ffunction-sections -fdata-sections -pthread")

# 设置执行文件输出目录
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/../../bin/")

# 根据选项设置 Release 和 Debug 版本的选项
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    # Release 版本
    message("Building Release version")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O3 -DNDEBUG -Wall -Wextra")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    message("Building Debug version")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wall -Wextra -pg")
else()
    # Debug 版本
    message("Building Debug Memory version")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wall -Wextra  -pg -fsanitize=address -fsanitize-recover=address")
endif()

# 指定源文件目录
aux_source_directory("${CMAKE_CURRENT_SOURCE_DIR}" SOURCES)
aux_source_directory("${CMAKE_CURRENT_SOURCE_DIR}/../3rd/cgzSource" SOURCES)

# 添加头文件目录 
include_directories(
    /usr/local/include
    /usr/include/mysql
    /usr/local/include/cpprest
    /usr/local/include/msgpack
    /usr/local/include/clickhouse
    /usr/local/include/clickhouse/absl
    /usr/local/include/mongocxx/v_noabi
    /usr/local/include/bsoncxx/v_noabi
    /usr/local/include/bsoncxx/v_noabi/bsoncxx/third_party/mnmlstc
    ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/lua/src
    ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/cgzInclude
)

# 添加可执行文件
add_executable(${PROJECT_NAME} ${SOURCES})

# 查找 OpenSSL 库
find_package(OpenSSL REQUIRED)

# 查找 MySQL 客户端库
find_library(MYSQL_LIB mysqlclient HINTS /usr/lib/x86_64-linux-gnu)

# 查找 sqlite3 库
find_package(SQLite3 REQUIRED)

# 查找 pkg-config
find_package(PkgConfig REQUIRED)

# 查找 mongo-cxx-driver 和 bsoncxx
pkg_check_modules(MONGOCXX REQUIRED libmongocxx)
pkg_check_modules(BSONCXX REQUIRED libbsoncxx)

# 查找 spdlog
find_package(spdlog REQUIRED)

# 添加 msgpack 的路径
find_package(msgpack-cxx REQUIRED)

# 查找 fmt
find_package(fmt REQUIRED)

# ==============================================
# ClickHouse-CPP 手动配置（核心修改部分）
# ==============================================
# 设置 ClickHouse 库路径
set(CLICKHOUSE_CPP_LIB "/usr/local/lib/libclickhouse-cpp-lib.a")
set(CLICKHOUSE_CPP_INCLUDE "/usr/local/include")

# 创建导入目标
add_library(clickhouse-cpp STATIC IMPORTED)
set_target_properties(clickhouse-cpp PROPERTIES
    IMPORTED_LOCATION "${CLICKHOUSE_CPP_LIB}"
    INTERFACE_INCLUDE_DIRECTORIES "${CLICKHOUSE_CPP_INCLUDE}"
)

# 查找 ClickHouse 依赖库
find_library(CITYHASH_LIB cityhash PATHS /usr/local/lib REQUIRED)
find_library(LZ4_LIB lz4 PATHS /usr/local/lib REQUIRED)
find_library(ZSTD_LIB zstdstatic PATHS /usr/local/lib REQUIRED)
find_library(ABSL_INT128_LIB absl_int128 PATHS /usr/local/lib REQUIRED)
# ==============================================

# 添加头文件目录
link_directories(${CPPREST_LIBRARY_DIRS})

# 连接相关库文件
set(LIBRARY_LIST 
    OpenSSL::SSL 
    OpenSSL::Crypto 
    pthread 
    z 
    rt 
    jemalloc 
    lua
    cpprest 
    ssl 
    crypto
    SQLite::SQLite3 
    ${MYSQL_LIB} 
    ${MONGOCXX_LIBRARIES} 
    ${BSONCXX_LIBRARIES}
    hiredis
    spdlog::spdlog
    msgpack-cxx
    fmt::fmt
    dl
    # 添加 ClickHouse 相关库
    clickhouse-cpp
    ${CITYHASH_LIB}
    ${LZ4_LIB}
    ${ZSTD_LIB}
    ${ABSL_INT128_LIB}
)

target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBRARY_LIST})